package com.junpuxin.mapper;

import com.junpuxin.dto.SchoolPageDTO;
import com.junpuxin.entity.School;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author junpuxin
 */
@Repository
public interface SchoolMapper {

    @Insert("""
        insert into rms_school
        (`title`, `college`, `major`, `clazz`, `address`, `info`, `version`, `deleted`, `created`, `updated`)
        values 
        (#{title}, #{college}, #{major}, #{clazz}, #{address}, #{info}, #{version}, #{deleted}, #{created}, #{updated})
        """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(School school);

    @Select("""
            select * from rms_school t
            where t.id = #{param1} and t.deleted = 0
            """)
    School select(Long id);

    @Select("""
            <script>
            select * from rms_school t
            <where>
                <if test='title != null'> title like concat('%', #{title}, '%') and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<School> list(SchoolPageDTO dto);

    @Update("""
            <script>
            update rms_school
            <set>
               <if test = 'title != null'> title = #{title}, </if>
               <if test = 'college != null'> college = #{college}, </if>
               <if test = 'major != null'>major = #{major},</if>
               <if test = 'clazz != null'>clazz = #{clazz},</if>
               <if test = 'address != null'>address = #{address},</if>
               <if test = 'info != null'>info = #{info},</if>
               <if test = 'version != null'>version = #{version},</if>
               <if test = 'deleted != null'>deleted = #{deleted},</if>
               <if test = 'created != null'>created = #{created},</if>
               <if test = 'updated != null'>updated = #{updated},</if>
               version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """)
    int update(School school);

    @Update("""
            update rms_school set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update rms_school set deleted = 1, updated = current_timestamp where 
            <foreach collection="list" item="e" open="id in (" close=")" separator=",">
                ${e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);
}
